<?php
/*
 * Created on 28-04-2011
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 require_once('Codes.php');
 require_once('DS.php');
 require_once('Entanglement.php');
 
 if(!empty($_POST)){
  	if(!isset($_POST['xml'])){
    	return constant("ARGUMENT_NULL").':xml';
  	}
  $xml = $_POST['xml'];
  
    if(!isset($_POST['userId'])){
    	return constant("ARGUMENT_NULL").':userId';
  	}
  $userId = $_POST['userId'];
  
  
  } else{
	return constant("ARGUMENT_NULL").':ALL';
}
$xml = stripslashes($xml);
$x = new SimpleXMLElement($xml);
$type = $x->Type[0];
$typeAr = explode(".", $type);
if(count($typeAr) != 3){
	die(constant("MESSAGE_TYPE_INVALID").':Expected three elements');
}
if($typeAr[0] != 'ENTANGLEMENT'){
	die(constant("MESSAGE_TYPE_INVALID").':Expected (ENTANGLEMENT) message source');
}
if($typeAr[1] == 'TECHREQ'){
	require_once("TechReqProcessor.php");
	$payload = $x->Payload[0];
	exit(Process($typeAr[2],$payload));
}else{
	$eId = Entanglement::GetEID($userId);
	Open();
	$userId = mysql_real_escape_string($userId);
	$xml = mysql_real_escape_string($xml);
	$time = gmdate("Y-m-d H:i:s");
	$r = Run("INSERT INTO tbl_requests(E_ID,UserID,MsgDate,MsgXml) VALUES ('$eId','$userId', '$time','$xml')");
	if($r != false){
		echo constant("MESSAGE_SAVED");
	} else{
		echo constant("TECH_ERROR").':'.mysql_error();
	}
	Close();
}
?>
